/*
 * CustomerAction.java
 * 
 * Created on 02/10/2007, 12:43:14 PM
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.bean.Asamblea;
import model.bean.AsistenteAsamblea;
import model.bean.Mocion;
import model.bean.Socio;
import model.bean.SolicitudAsamblea;

import model.factory.RecreacionalFactory;
import model.service.AsambleaService;
import model.service.AsistenteAsambleaService;
import model.service.MocionService;
import model.service.SocioService;
import model.service.SolicitudAsambleaService;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.xalan.xsltc.runtime.Parameter;
import view.common.Constants;
import view.common.struts.CustomActionForm;

import view.common.struts.CustomDispatchAction;

/**
 * @Archivo LoginAction.java
 * @Fecha                @Autor   @Cambios
 * 21/01/2011 02:35:29 PM       moshiro
 */
public class AsambleaAction extends CustomDispatchAction {

    private final static String PRINCIPAL = "principal";
    //private final static String REGISTRAR = "registrar";
    private final static String MODIFICAR = "modificar";
    private final static String LISTAR = "listar";
    private final static String PRINCIPAL_LISTA_ASISTENTE = "principalListaAsistente";
    private final static String REGISTRAR_LISTA_ASISTENTE = "registrarListaAsistente";
    private final static String PRINCIPAL_MOCION = "principalMocion";
    private final static String REGISTRAR_MOCION = "registrarMocion";
    private final static String MODIFICAR_MOCION = "modificarMocion";

    @Override
    public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Asamblea beanSearch = new Asamblea();
        frm.setBeanSearch(beanSearch);
        frm.setBeanList(getAsambleaService().listarAsamblea(beanSearch));
        frm.setMapList(new HashMap());
        frm.setReadOnly(null);
        return mapping.findForward(PRINCIPAL);
    }

    public ActionForward consultar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Asamblea beanSearch = (Asamblea) frm.getBeanSearch();
        frm.setBeanList(getAsambleaService().listarAsamblea(beanSearch));
        return mapping.findForward(PRINCIPAL);
    }

    public ActionForward refrescar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        return mapping.findForward(PRINCIPAL);
    }

    public ActionForward registrar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Asamblea bean = new Asamblea();
        bean.setSolicitudAsamblea(new SolicitudAsamblea());
        frm.setBean(bean);
        SolicitudAsamblea filtro = new SolicitudAsamblea();
        filtro.setEstado(Constants.ESTADO_NUEVO);
        frm.setReadOnly("N");
        frm.getMapList().put("solicitudAsambleaList", getSolicitudAsambleaService().listarSolicitudAsamblea(filtro));
        return mapping.findForward(MODIFICAR);
    }

    public ActionForward eliminar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        getAsambleaService().eliminarAsamblea(Long.valueOf(frm.getSelectedIndex()));
        return unspecified(mapping, form, req, resp);
    }

    public ActionForward modificar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Asamblea beanSelected = new Asamblea(Long.valueOf(frm.getSelectedIndex()));
        Asamblea bean = getAsambleaService().obtenerAsamblea(beanSelected);
        //setTotalAsistentes(bean);
        frm.setBean(bean);
        frm.setReadOnly("M");
        return mapping.findForward(MODIFICAR);
    }

//    private void setTotalAsistentes(Asamblea asamblea) throws Exception {
//        AsistenteAsamblea beanSearch = new AsistenteAsamblea();
//        beanSearch.setAsisitio(1);
//        beanSearch.getAsamblea().setCodigo(asamblea.getCodigo());
//        int totalAsistentes = 0;
//        try {
//            totalAsistentes = getAsistenteAsambleaService().listarAsistenteAsamblea(beanSearch).size();
//        } catch (Exception ex) {
//        }
//        asamblea.setTotalAsistentes(totalAsistentes);
//    }
    public ActionForward retroceder(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Asamblea bean = (Asamblea) frm.getBean();
        if (bean.getCodigo() != null && frm.getReadOnly() != null && frm.getReadOnly().equals("N")) {
            Mocion beanSearch = new Mocion();
            beanSearch.getAsamblea().setCodigo(bean.getCodigo());
            if (getMocionService().listarMocion(beanSearch).isEmpty()) {
                getAsambleaService().eliminarAsamblea(Long.valueOf(frm.getSelectedIndex()));
            }
        }
        return unspecified(mapping, form, req, resp);
    }

    public ActionForward cargar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Asamblea beanSelected = new Asamblea(Long.valueOf(frm.getSelectedIndex()));
        Asamblea bean = getAsambleaService().obtenerAsamblea(beanSelected);
        //setTotalAsistentes(bean);
        frm.setBean(bean);
        return mapping.findForward(MODIFICAR);
    }

    public ActionForward listar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Asamblea beanSelected = new Asamblea(Long.valueOf(frm.getSelectedIndex()));
        Asamblea bean = getAsambleaService().obtenerAsamblea(beanSelected);
        frm.setBean(bean);

        AsistenteAsamblea filtro = new AsistenteAsamblea();
        filtro.setAsamblea(new Asamblea(beanSelected.getCodigo()));
        frm.setFormFileList(getAsistenteAsambleaService().listarAsistenteAsamblea(filtro));

        Mocion mocion = new Mocion();
        mocion.setAsamblea(new Asamblea(beanSelected.getCodigo()));
        frm.setBeanList(getMocionService().listarMocion(mocion));
        return mapping.findForward(LISTAR);
    }

    public ActionForward guardar(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Asamblea bean = (Asamblea) frm.getBean();

        if (bean.getCodigo() == null) {
            //INSERTAR
            bean.setEstado(Constants.ESTADO_PROCESO);
            getAsambleaService().agregarAsamblea(bean);
            frm.setSelectedIndex("" + bean.getCodigo());
            return cargar(mapping, form, req, resp);
            //return mapping.findForward(REGISTRAR);
//            return unspecified(mapping, form, req, resp);
        } else {
            //MODIFICAR
            getAsambleaService().modificarAsamblea(bean);
            frm.setSelectedIndex("" + bean.getCodigo());
            return cargar(mapping, form, req, resp);
        }
    }

    //Lista de Asistentes
    public ActionForward cargarListaAsistentes(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;

//        if (((Asamblea) frm.getBean()).getCodigo() != null) {
        AsistenteAsamblea beanSearch = new AsistenteAsamblea();
        beanSearch.getAsamblea().setCodigo(((Asamblea) frm.getBean()).getCodigo());
        List<AsistenteAsamblea> list = getAsistenteAsambleaService().listarAsistenteAsamblea(beanSearch);
        frm.setBeanDetailList(list);
//        } else{
//            frm.setBeanDetailList(new ArrayList<AsistenteAsamblea>());
//        }

        return mapping.findForward(PRINCIPAL_LISTA_ASISTENTE);
    }

    public ActionForward registrarListaAsistentes(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        List<Socio> nuevaLista = new ArrayList<Socio>();
        List<Socio> listaPrincipal = getSocioService().listarSocio(new Socio());
        List<AsistenteAsamblea> listaSecundaria = frm.getBeanDetailList();
        for (Socio bean : listaPrincipal) {
            boolean existe = false;
            for (AsistenteAsamblea bean2 : listaSecundaria) {
                if (bean.getCodigo().longValue() == bean2.getSocio().getCodigo().longValue()) {
                    existe = true;
                }
            }
            if (!existe) {
                nuevaLista.add(bean);
            }
        }
        frm.setBeanDetailList2(nuevaLista);
        return mapping.findForward(REGISTRAR_LISTA_ASISTENTE);
    }

    public ActionForward refrescarSocio(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        return mapping.findForward(REGISTRAR_LISTA_ASISTENTE);
    }

    public ActionForward guardarListaAsistentes(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        List<Socio> lista = frm.getBeanDetailList2();
        String[] socios = req.getParameterValues("socios");
        if(socios!=null)
        {
            for (String i : socios) {
                AsistenteAsamblea bean = new AsistenteAsamblea();
                Socio socio = (Socio) lista.get(Integer.parseInt(i));
                bean.getSocio().setCodigo(socio.getCodigo());
                bean.getAsamblea().setCodigo(((Asamblea) frm.getBean()).getCodigo());
                bean.setAsisitio(0);
                getAsistenteAsambleaService().agregarAsistenteAsamblea(bean);
            }
        }
//        for (int i = 0; i < lista.size(); i++) {
//            if (req.getParameter("socio" + i) != null) {
//                AsistenteAsamblea bean = new AsistenteAsamblea();
//                Socio socio = (Socio) lista.get(i);
//                bean.getSocio().setCodigo(socio.getCodigo());
//                bean.getAsamblea().setCodigo(((Asamblea) frm.getBean()).getCodigo());
//                bean.setAsisitio(0);
//                getAsistenteAsambleaService().agregarAsistenteAsamblea(bean);
//            }
//        }
        return cargarListaAsistentes(mapping, form, req, resp);
    }

    public ActionForward eliminarListaAsistentes(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        getAsistenteAsambleaService().eliminarAsistenteAsamblea(Long.valueOf(frm.getSelectedIndex2()));
        return cargarListaAsistentes(mapping, form, req, resp);
    }

    public ActionForward guardarAsistencia(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        List<AsistenteAsamblea> lista = frm.getBeanDetailList();
        int totalAsistio = 0;
        for (AsistenteAsamblea bean : lista) {
            if (req.getParameter("asistente" + bean.getCodigo()) != null) {
                bean.setAsisitio(1);
                totalAsistio++;
            } else {
                bean.setAsisitio(0);
            }
            getAsistenteAsambleaService().modificarAsistenteAsamblea(bean);
        }
        Asamblea asamblea = (Asamblea) frm.getBean();
        if (totalAsistio > (lista.size() / 2)) {
            asamblea.setEstado(Constants.ESTADO_PROCESO);
        } else {
            asamblea.setEstado(Constants.ESTADO_NO_VALIDO);
        }
        frm.setSelectedIndex("" + asamblea.getCodigo());
        getAsambleaService().modificarAsamblea(asamblea);

        //return cargarListaAsistentes(mapping, form, req, resp);
        return cargar(mapping, form, req, resp);
    }

    //Mocion
    public ActionForward cargarMocion(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Mocion beanSearch = new Mocion();
        beanSearch.getAsamblea().setCodigo(((Asamblea) frm.getBean()).getCodigo());
        frm.setBeanDetailList(getMocionService().listarMocion(beanSearch));
        //Total de asistentes
        AsistenteAsamblea beanSearch2 = new AsistenteAsamblea();
        beanSearch2.setAsisitio(1);
        beanSearch2.getAsamblea().setCodigo(((Asamblea) frm.getBean()).getCodigo());
        int totalAsistentes = getAsistenteAsambleaService().listarAsistenteAsamblea(beanSearch2).size();
        req.setAttribute("totalAsistentes", totalAsistentes);
        return mapping.findForward(PRINCIPAL_MOCION);
    }

    public ActionForward refrescarMocion(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        return mapping.findForward(PRINCIPAL_MOCION);
    }

    public ActionForward registrarMocion(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Mocion bean = new Mocion();
        bean.getAsamblea().setCodigo(((Asamblea) frm.getBean()).getCodigo());
        frm.setBeanDetail(bean);
        return mapping.findForward(REGISTRAR_MOCION);
    }

    public ActionForward eliminarMocion(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        getMocionService().eliminarMocion(Long.valueOf(frm.getSelectedIndex2()));
        return cargarMocion(mapping, form, req, resp);
    }

    public ActionForward modificarMocion(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Mocion beanSelected = new Mocion(Long.valueOf(frm.getSelectedIndex2()));
        Mocion bean = getMocionService().obtenerMocion(beanSelected);
        frm.setBeanDetail(bean);
        return mapping.findForward(MODIFICAR_MOCION);
    }

    public ActionForward guardarMocion(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
        Mocion bean = (Mocion) frm.getBeanDetail();
        if (req.getParameter("beanDetail.modificaEstatuto") != null) {
            bean.setModificaEstatuto(1);
        }

        if (bean.getCodigo() == null) {
            //INSERTAR
            bean.setEstado(0);
            getMocionService().agregarMocion(bean);
        } else {
            //MODIFICAR
            getMocionService().modificarMocion(bean);
        }
        return cargarMocion(mapping, form, req, resp);
    }

    public ActionForward aprobarMocion(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse resp) throws Exception {
        CustomActionForm frm = (CustomActionForm) form;
//        List<Mocion> lista = frm.getBeanDetailList();
//        for (Mocion bean : lista) {
//            if (req.getParameter("mocion" + bean.getCodigo()) != null) {
//                bean.setEstado(1);
//            } else {
//                bean.setEstado(0);
//            }
//            getMocionService().modificarMocion(bean);
//        }


        Asamblea asamblea = (Asamblea) frm.getBean();
        int totalAsistentes = asamblea.getTotalAsistentes();

        List<Mocion> lista = frm.getBeanDetailList();
        int totalVotado = 0;
        for (Mocion bean : lista) {
            if ((bean.getaFavor() > 0) || (bean.getEnContra() > 0)) {
                bean.setAbstienen(totalAsistentes - bean.getaFavor().longValue() - bean.getEnContra().longValue());
                bean.setEstado(Constants.ESTADO_VOTADO);
                totalVotado++;
            } else {
                bean.setAbstienen(0L);
                bean.setEstado(Constants.ESTADO_NUEVO);
            }
            getMocionService().modificarMocion(bean);
        }

        if (totalVotado == lista.size()) {
            asamblea.setEstado(Constants.ESTADO_FINALIZADO);
            getAsambleaService().modificarAsamblea(asamblea);
        }
        frm.setSelectedIndex("" + asamblea.getCodigo());
        //return cargarMocion(mapping, form, req, resp);
        return cargar(mapping, form, req, resp);
    }

    public AsambleaService getAsambleaService() {
        return RecreacionalFactory.getInstance().getAsambleaService();
    }

    public SolicitudAsambleaService getSolicitudAsambleaService() {
        return RecreacionalFactory.getInstance().getSolicitudAsambleaService();
    }

    public AsistenteAsambleaService getAsistenteAsambleaService() {
        return RecreacionalFactory.getInstance().getAsistenteAsambleaService();
    }

    public SocioService getSocioService() {
        return RecreacionalFactory.getInstance().getSocioService();
    }

    public MocionService getMocionService() {
        return RecreacionalFactory.getInstance().getMocionService();
    }
}
